import 'package:educate/pages/home/home.dart';
import 'package:educate/pages/my/my.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get.dart';
import '../../common/config.dart';
import 'main_controller.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return _HomeState();
  }
}

class _HomeState extends GetView<MainController> {
  // 主视图
  Widget _buildView() {
    final MainController controller = Get.find<MainController>();
    return Scaffold(
      extendBody: true,
      floatingActionButton: Padding(
        padding: const EdgeInsets.only(top: 40.0),
        child: _buildFloatingActionButton(),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      body: pageView(controller),
      bottomNavigationBar: _buildBottomNavigationBar(),
    );
  }

  Theme _buildBottomNavigationBar() {
    return Theme(
      //去掉水波纹效果
      data: ThemeData(
        brightness: Brightness.light,
        splashColor: Colors.transparent,
        highlightColor: Colors.transparent,
      ),
      child: ClipRRect(
        borderRadius: const BorderRadius.vertical(
          top: Radius.circular(35),
        ),
        child: GetBuilder<MainController>(
          id: 'navigation',
          builder: (controller) {
            return SizedBox(
              height: 70.h,
              child: BottomNavigationBar(
                type: BottomNavigationBarType.fixed,
                showUnselectedLabels: true,
                items: [
                  BottomNavigationBarItem(
                      icon: Text(
                        "首页",
                        style: TextStyle(
                            color: const Color(0xFFB4B4B4), fontSize: 16.sp),
                      ),
                      activeIcon: Text(
                        "首页",
                        style: TextStyle(
                            color: const Color(0xFF3B84FF), fontSize: 16.sp),
                      ),
                      label: ""),
                  BottomNavigationBarItem(
                      icon: Text(
                        "文件",
                        style: TextStyle(
                            color: const Color(0xFFB4B4B4), fontSize: 16.sp),
                      ),
                      activeIcon: Text(
                        "文件",
                        style: TextStyle(
                            color: const Color(0xFF3B84FF), fontSize: 16.sp),
                      ),
                      label: ""),
                  BottomNavigationBarItem(icon: Container(), label: ""),
                  BottomNavigationBarItem(
                      icon: Text(
                        "发现",
                        style: TextStyle(
                            color: const Color(0xFFB4B4B4), fontSize: 16.sp),
                      ),
                      activeIcon: Text(
                        "发现",
                        style: TextStyle(
                            color: const Color(0xFF3B84FF), fontSize: 16.sp),
                      ),
                      label: ""),
                  BottomNavigationBarItem(
                      icon: Text(
                        "我的",
                        style: TextStyle(
                            color: const Color(0xFFB4B4B4), fontSize: 16.sp),
                      ),
                      activeIcon: Text(
                        "我的",
                        style: TextStyle(
                            color: const Color(0xFF3B84FF), fontSize: 16.sp),
                      ),
                      label: ""),
                ],
                currentIndex: controller.currentIndex,
                onTap: (index) {
                  controller.onJumpToPage(index);
                },
              ),
            );
          },
        ),
      ),
    );
  }

  PageView pageView(MainController controller) {
    return PageView(
      physics: const NeverScrollableScrollPhysics(),
      controller: controller.pageController,
      onPageChanged: controller.onIndexChanged,
      children: const [
        Home(),
        Center(
          child: Text(
            "2",
            style: TextStyle(fontSize: fontSize),
          ),
        ),
        Center(
          child: Text(
            "3",
            style: TextStyle(fontSize: fontSize),
          ),
        ),
        Center(
          child: Text(
            "4",
            style: TextStyle(fontSize: fontSize),
          ),
        ),
        My(),
      ],
    );
  }

  Widget _buildFloatingActionButton() {
    return Padding(
      padding: const EdgeInsets.only(top: 2.0),
      child: RawMaterialButton(
        onPressed: () {},
        elevation: 0,
        fillColor: Colors.white,
        padding: const EdgeInsets.all(3.0),
        shape: const CircleBorder(),
        child: Stack(
          children: [
            SvgPicture.asset("assets/svgs/1.svg"),
            Image.asset("assets/images/2.png"),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainController>(
      init: MainController(),
      id: "main",
      builder: (_) => _buildView(),
    );
  }
}
